package demo.practice.uf;

public class P695 {


    public int maxAreaOfIsland(int[][] grid) {

        int h = grid.length;
        int w = grid[0].length;
        UFRankUnionPlant uf = new UFRankUnionPlant(w * h);
        boolean noJoin = true;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                //int index = w * i + j;
                if (grid[i][j] == 1) {
                    noJoin = false;
                    //做合并操作
                    if (i - 1 >= 0 && grid[i - 1][j] == 1) {
                        uf.join(w * (i - 1) + j, w * i + j);
                    }

                    if (j - 1 >= 0 && grid[i][j - 1] == 1) {
                        uf.join(w * i + j - 1, w * i + j);
                    }
                }
            }
        }
        return  noJoin? 0:  uf.maxLen;
    }
}
